package main

import (
	mysql "aqielife.cn/spider/db"
	"aqielife.cn/spider/models"
	"aqielife.cn/spider/utils"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"log"
)

var (
	BaseUrl = "http://edu.people.com.cn"
)

func parseList(url string) []models.ArticleDetail {
	err, doc := utils.DoRequest(url)
	if err != nil {
		log.Fatal(err)
	}
	var pages []models.ArticleDetail
	// body > div.column_3.column_3b.column_3c.mt20 > div:nth-child(1) > ul
	// body > div.column_3.column_3b.column_3c.mt20 > div.left.w310.center > ul
	find := doc.Find("ul.list_14b > li")
	find.Each(func(i int, s *goquery.Selection) {
		url, _ := s.Find("a").Attr("href")
		id := utils.GenID()
		pages = append(pages, models.ArticleDetail{
			ID:  id,
			Url: url,
		})
		parseDetail(BaseUrl+url, id)
	})
	return pages
}

func parseDetail(url string, id int64) *models.ArticleDetail {
	err, doc := utils.DoRequest(url)
	if err != nil {
		log.Fatal(err)
	}
	title := doc.Find("div.col.col-1.fl > h1").Text()
	content := doc.Find("div.rm_txt_con.cf > p").Text()

	detail := models.ArticleDetail{
		ID:      id,
		Url:     url,
		Title:   title,
		Content: utils.DeleteExtraSpace(content),
		Type:    utils.EDU,
	}

	if err := mysql.DB.Create(&detail).Error; err != nil {
		log.Printf("db.Create err : %v", err)
	} else {
		log.Printf("新增成功: %d\n", id)
	}
	return &detail
}

func Start() {
	if err := utils.InitSnowFlake("2021-12-03", 1); err != nil {
		fmt.Println("Init() failed, err = ", err)
		return
	}
	pages := parseList(BaseUrl)
	log.Println("pages", pages)
}

func main() {
	Start()
}
